package com.ka.fragment;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ka.datacenter.DatabaseHandler;
import com.ka.lifemanagement.R;
import com.ka.model.GeneralModel;
import com.ka.provider.Type;
import com.ka.provider.Type.SunTime;
import com.ka.ui.Component01;
import com.ka.ui.Component01.Component01Delegate;

public class NowFragment extends GeneralFragment implements Component01Delegate {

	protected View contentView;
	protected TextView now_time;
	protected Button bt_calculator;
	protected LinearLayout morning_bounder;
	protected LinearLayout noon_bounder;
	protected LinearLayout evening_bounder;

	protected List<Component01> listComponent;
	protected Component01 stackHolder;
	protected DatabaseHandler database;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		contentView = inflater.inflate(R.layout.fragment_now, container, false);
		return contentView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initial();
		execution();

	}

	private void initial() {
		stackHolder = null;
		now_time = (TextView) contentView.findViewById(R.id.now_time);

		morning_bounder = (LinearLayout) contentView
				.findViewById(R.id.morning_bouder);
		noon_bounder = (LinearLayout) contentView
				.findViewById(R.id.noon_bouder);
		evening_bounder = (LinearLayout) contentView
				.findViewById(R.id.evening_bouder);

		bt_calculator = (Button) contentView.findViewById(R.id.bt_calculator);

		listComponent = new ArrayList<Component01>();

		database = new DatabaseHandler(getActivity());
	}

	private void execution() {
		DateFormat df = new SimpleDateFormat(" dd - MM - yyyy",
				Locale.getDefault());
		String date = df.format(Calendar.getInstance().getTime());
		now_time.setText(date);

		bt_calculator.setOnClickListener(calculatorEvent);

		morning_bounder.setTag(Type.SunTime.Morning);
		noon_bounder.setTag(Type.SunTime.Noon);
		evening_bounder.setTag(Type.SunTime.Evening);

		addComponent(morning_bounder);
		addComponent(noon_bounder);
		addComponent(evening_bounder);
	}

	protected void addComponent(View v) {

		Component01 com = new Component01(contentView.getContext(),
				(SunTime) v.getTag());

		com.delegate = this;
		((LinearLayout) v).addView(com);
		listComponent.add(com);
	}

	public View calculatorViewFromSuntime(SunTime suntime) {

		switch (suntime) {
		case Morning:
			return morning_bounder;
		case Noon:
			return noon_bounder;
		case Evening:
			return evening_bounder;
		default:
			return null;
		}
	}

	@Override
	public void addNewComponent(SunTime suntime) {
		addComponent(calculatorViewFromSuntime(suntime));
	}

	OnClickListener calculatorEvent = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int total = 0;

			List<GeneralModel> lmo = new ArrayList<GeneralModel>();
			for (Component01 com : listComponent) {
				total += com.getValue();
				lmo.add(com.getModel());
			}

			Toast.makeText(contentView.getContext(), total + "  VND",
					Toast.LENGTH_LONG).show();
		}
	};

	@Override
	public void removeComponent(Component01 com) {

		((ViewGroup) com.getParent()).removeView(com);
		listComponent.remove(com);
		stackHolder = com;
	}
}
